#!/bin/bash

#this file change the sparse matrix format from:
#1 2 0.3
#1 3 0.2
#2 1 0.2
#2 2 0.1
#
# to 
#2:0.3 3:0.2
#1:0.3 2:0.1
#
#this format is suitable for redsvd

if [ $# -lt 1 ]
then
    echo "Usage: changeMatrixFormatV1.sh srcFile"
    exit 1
else
    srcFile=$1
fi

awk 'BEGIN{
#    print "298866 21029 4744675";
}
{
    $2=$2-1;
    if (1 == NR) {
        currentRow = $1-1;
        printf($2":"$3);
    }
    else if(($1-1) == currentRow) {
        printf(" "$2":"$3);
    }
    else {
        printf("\n");
        emptyLines = $1-1-currentRow-1;
        for (i=0; i< emptyLines; ++i) {
            print "1:0";
        }
        printf($2":"$3);
        currentRow = $1-1;
    }
}
END {
    printf("\n");
}' $srcFile
